package com.tarena.lbs.content.service.impl;

import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.dao.repository.CategoryRepository;
import com.tarena.lbs.content.pojo.param.ArticleCategoryParam;
import com.tarena.lbs.content.pojo.vo.ArticleCategoryVO;
import com.tarena.lbs.content.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryRepository categoryRepository;
    /*
    * 新增分类
    * */
    @Override
    public void addCategory(ArticleCategoryParam articleCategoryParam) {
        ArticleCategoryParam param=new ArticleCategoryParam();
        BeanUtils.copyProperties(articleCategoryParam,param);
        categoryRepository.addCategory(param);
    }

    /*
    * 查询分类列表
    * */
    @Override
    public Result<PageResult<ArticleCategoryVO>> selectCategory(String categoryName,Integer categoryStatus) {
        PageResult<ArticleCategoryVO> pageResult=new PageResult<>();
        pageResult.setTotal(categoryRepository.selectCategoryCount(categoryName,categoryStatus));
        pageResult.setObjects(categoryRepository.selectCategory(categoryName,categoryStatus));
        pageResult.setPageSize(10);
        return new Result<>(pageResult);
    }

    /*
    * 修改分类
    * */
    @Override
    public Result<Integer> updateCategory(ArticleCategoryParam articleCategoryParam) {
        int num = categoryRepository.updateCategory(articleCategoryParam);
        log.debug("修改分类数量：{}",num);
        return new Result<>(num);
    }

    /*
    * 删除分类
    * */
    @Override
    public void deleteCategory(ArticleCategoryParam articleCategoryParam) {
//        ArticleCategoryParam articleCategoryParam=new ArticleCategoryParam();
//        articleCategoryParam.setCategoryStatus(2);
//        articleCategoryParam.setId(id);
        articleCategoryParam.setCategoryStatus(2);
        log.debug("删除分类参数：{}",articleCategoryParam);
        categoryRepository.updateCategory(articleCategoryParam);
    }
}
